package lukeer.fencing;

import csproj.fencing.Move;
import csproj.fencing.Player;

import java.io.PrintStream;
import java.util.Random;

/**
 * Provides declarations of most methods in Player. All other players inherit from this class and override makeMove.
 * This class is just a superclass of all other player classes, it does not generate moves.
 *
 * User: Luke Richardson, Wafa M'Barek
 * Date: 05/10/12
 * Time: 21:13
 */
public class BasicPlayer implements Player, csproj.fencing.Piece {
    static final int VERTICAL = 1;
    static final int HORIZONTAL = 2;
    final Random r = new Random();
    Board currentGameState;
    int player;
    int numberOfMoves;

    /**
     * Returns the winner of the currentGameState for the player class
     *
     * @return int value of board winner
     */
    public int getWinner() {
        currentGameState.calculateResult();
        return currentGameState.getResult();
    }

    /**
     * Sets up board and assigns a piece type to this player
     *
     * @param n width of board to be initialised
     * @param p player piece assigned
     * @return -1 if error occurs, 0 otherwise
     */
    public int init(int n, int p) {
        try {
            currentGameState = new Board(n);
        } catch (FencingException e) {
            return INVALID;
        }
        player = p;
        return 0;
    }

    /**
     * makeMove for BasicPlayer is not implemented
     *
     * @return null
     */
    @Override
    public Move makeMove() {
        return null;
    }


    /**
     * Checks that the opponent move played is valid and places piece on currentGameState board
     *
     * @param m move object created by opponent player
     * @return -1 if move is invalid, 0 if is valid
     */
    public int opponentMove(Move m) {
        numberOfMoves++;
        try {
            currentGameState.placePiece(m);
        } catch (FencingException e) {
            System.err.println("Illegal move made by opponent");
            System.err.println(e.getMessage());
            return -1;
        }
        return 0;
    }

    /**
     * Return a string representation of the currentGameState board
     *
     * @param output PrintStream output object (console)
     */
    public void printBoard(PrintStream output) {
        output.print(currentGameState.getBoard());
    }

}
